package feng.demo.controller.adm;

import feng.demo.entity.Hot;
import feng.demo.entity.Type;
import feng.demo.entity.XJson;
import feng.demo.service.HotService;
import feng.demo.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/adm/hot")
public class AdmHotController {
    @Autowired
    HotService hotService;
    @Autowired
    TypeService typeService;

    @GetMapping("/findAll")
    public String findAll(){
//        查询全部热度图片
        List<Hot> hots = hotService.findAll();

        return new XJson(200,"",hots).toString();
    }

    @GetMapping("/del/{id}")
    public String del(@PathVariable("id")Integer typeid){
//        删除热度图片
        int del = hotService.del(typeid);
        if(del<1)return new XJson(401,"删除失败","").toString();
        List<Hot> hots = hotService.findAll();
        return new XJson(200,"删除成功",hots).toString();
    }

    @GetMapping("/refresh")
    public String refresh(){
//        刷新热度图片                待完成
        List<Hot> hots = hotService.findAll();
        return new XJson(200,"",hots).toString();
    }
    @GetMapping("/findTypes/{p}/{l}")
    public String findTypes(@PathVariable("p")Integer p,@PathVariable("l")Integer l){

        Map<String,Object> map =new HashMap<>();
        List<Hot> hots = hotService.findAll();
        List<Type> list = typeService.findAllLimit(p, l);
        int count = typeService.findCount();
        map.put("type",list);
        map.put("count",count);
        map.put("hot",hots);
        return new XJson(200,"",map).toString();
    }

    @GetMapping("/add/{id}")
    public String  add(@PathVariable("id")Integer typeid){
        Hot hot = new Hot();
        Type type = typeService.findById(typeid);
        hot.setMemo(type.getTypename());
        hot.setHeat(type.getTypeid());
        hot.setType(type);
        hot.setPic(type.getPic());
        hotService.add(hot);
        return new XJson(200,"添加成功","").toString();
    }
}
